package com.ruoyi.dida.db.manager.controller;

import com.ruoyi.common.utils.ReturnT;

import com.ruoyi.dida.db.manager.entity.ColumnInfo;
import com.ruoyi.dida.db.manager.entity.TableInfo;
import com.ruoyi.dida.db.manager.service.DBService;
import com.ruoyi.framework.web.controller.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Api(tags = "数据表接口")
@Controller
@RequestMapping("/db/table")
public class TableController extends BaseController {

    @Resource
    private DBService dbService;

    @GetMapping("/list")
    @ApiOperation("数据库中表列表")
    public ReturnT<List<TableInfo>> tableList(@RequestParam(required = true, defaultValue = "datax_web")String dbName){
        return new ReturnT<>(dbService.tableList(dbName));
    }

    // 接入方式：全部、数据库接入、实时接入、结构文本、数据开发、数据清洗
    // 状态：全部、正在执行、执行成功、执行失败
    @ApiOperation("指定数据库中查询表列表")
    @GetMapping("/search/tables")
    @ResponseBody
    public List<TableInfo> tableSearchList(@RequestParam(defaultValue = "big_data_manager")String dbName,
                                      @RequestParam(required = false)String tableName,
                                      @RequestParam(required = false,defaultValue = "0")String collectWay,
                                      @RequestParam(required = false,defaultValue = "0")String status){
        return dbService.tableSearchList(dbName,tableName,collectWay,status);
    }

    @GetMapping("/table/column")
    @ApiOperation("根据库名、表名--查询字段信息")
    public ReturnT<List<ColumnInfo>> tableColumnInfo(@RequestParam(defaultValue = "datax_web")String dbName,
                                                     @RequestParam String tableName){
        return new ReturnT<>(dbService.tableColumnInfo(dbName,tableName));
    }

    @PostMapping("/table")
    @ApiOperation("根据库名、表名--创建表")
    public ReturnT addTable(@RequestParam(defaultValue = "datax_web")String dbNameSource,
                            @RequestParam String tableNameSource,
                            @RequestParam String dbNameTarget,
                            @RequestParam String tableNameTarget){
        return dbService.addTable(dbNameSource,tableNameSource,dbNameTarget,tableNameTarget);
    }

    @GetMapping("/table/data")
    @ApiOperation("根据库名、表名--查询表数据")
    public ReturnT<List<?>> tableData(@RequestParam(defaultValue = "datax_web")String dbName,
                                      @RequestParam String tableName, @RequestParam(defaultValue = "10") int nums){
        return new ReturnT<>(dbService.tableData(dbName,tableName,nums));
    }

}
